package com.software.backend.searchmovie.controller;
import com.software.backend.searchmovie.entity.Movie;
import com.software.backend.searchmovie.entity.Video;
import com.software.backend.searchmovie.service.MovieService;
import com.software.backend.searchmovie.service.VideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@Component
@RestController
@RequestMapping("/searchMovie")
public class MovieController {

    @Autowired
    private MovieService movieService;
    @Autowired
    private VideoService videoService;
    @GetMapping("/findMovie")
    public List<Movie> fidMovies(@Valid String searchStr){
        videoService.cleanVideo();
        List<Movie> movies = movieService.searchMovies(searchStr);
//        System.out.println(movies);
        return movies;
    }
    @GetMapping("/findByMovieId")
    public List<Video> findVideos(@Valid int id){
        List<Video> videos = videoService.findVideoById(id);
        return videos;
    }
//    @Scheduled(cron = "* 0/5 * * * ? ")
//    public String cleanVideos(){
//            videoService.cleanVideo();
//            return "ok";
//    }
//    @GetMapping("/findMovie")
//    public void findMovies(@Valid List<Video> movieList){
//        //得得到path，即Movie查询到的电影名所对应的电影路径
//        for (int i = 0; i < movieList.size(); i++) {
//            for (int j = 0; j < movieList.get(i).getParts().size(); j++) {
//                Video movie = movieList.get(i);
//                Video.Part part = movie.getParts().get(j);
//                String startTime = part.getStartTime();
//                String endTime = part.getEndTime();
//                Integer duration = Integer.parseInt(endTime)-Integer.parseInt(startTime);
//                //path为电影的路径
////                String  path = movie.getPath();
//                FFmpegUtil fFmpegUtil = new FFmpegUtil();
//                try {
//                    fFmpegUtil.videoSpit("",startTime,duration.toString());
//                    //存放完成后在FFmepgUtil会自动放在一个路径上，命名通过UUID来实现。
//                } catch (IOException e) {
//                    throw new RuntimeException(e);
//                }
//            }
//        }
//
//    }
}
